#include "spectral_3d.h"
/* For explicit diffusion: 
		cfl = 0.07 is unstable, cfl = 0.05 is stable  

*/

int main( int argc, char **argv ){

	unsigned int 	p, q, r = 0;
	char 	 	num_file[80];
	struct input 	in;
	
	printf("%d\n",argc);
	if ((argc != 2)&&(argc != 3)) { 
		printf("Usage:\n\t %s\t input_file\tor\t%s input_file restart_file\n", argv[0],argv[0]); 
		exit(1);
	} 
	strcpy( in.run , argv[1] );
	load_consts(&in);
	printf("Heat equation \t yes/no\t\t\t\t%d\n",in.heat);
	if (argc == 3) read_binary(argv[2]);
	/*                          Evolution section				*/
	q = 0; p = 0;
	if (in.zakh == 0) linear_step( 0.5*cpow(-1.I,in.heat) );
	while (p*(in.dt) < in.Tmax) {		
	//	replace with  potential_step();  for linear Schrodinger with potential  
	//	replace with  nl_step();  	 for cubic Nonlinear Schrodinger 
	//	replace with  nnl_step();  	 for Nonlocal Nonlinear Schrodinger (NNLS)
		if (in.zakh == 0) {
		  nnl_step(1.*cpow(-1.I,in.heat));	// step due potential term:  
		  linear_step(1.*cpow(-1.I,in.heat));   // linear shrodinger with no potential	
		} else if (in.zakh == 1) ZS_step();
		p++;
		if (q == in.io_num) {
			q = 0;
			r++;
			if (in.zakh == 0) {
			  sprintf( num_file, "%s.psi.%04d", in.run, r );
			  linear_step( -0.5 *cpow(-1I,in.heat));
			  dump_grid_binary(num_file); // change to binary for long runs						
			  linear_step( 0.5*cpow(-1I,in.heat) );
			  printf ("Wrote %s time t=%f\tNum_particles = %.15e\n", num_file, p*(in.dt), particles());
			} else if (in.zakh == 1){
			  sprintf( num_file, "%s.psi.%04d", in.run, r );
			  z_dump_grid(num_file); // change to binary for long runs						
			  printf ("Wrote %s time t=%f\tNum_particles = %.15e\n", num_file, p*(in.dt), z_particles());
			}
		}
		q++;
	}	
	free_arrays();
	return 0;
}
